import SwiftUICore
struct RadarChartView: View {
    let data: [Double]
    let labels: [String]
    let color: Color
    
    var body: some View {
        GeometryReader { geometry in
            ZStack {
                RadarGridView(count: 5, size: geometry.size)
                RadarDataView(data: data, size: geometry.size, color: color)
                RadarLabelsView(labels: labels, size: geometry.size)
            }
        }
    }
}

struct RadarGridView: View {
    let count: Int
    let size: CGSize
    
    var body: some View {
        ZStack {
            ForEach(0..<5) { index in
                RadarPolygon(
                    count: count,
                    radius: size.width / 2.5 * Double(5 - index) / 5,
                    center: CGPoint(x: size.width / 2.5, y: size.width / 2.5)
                )
                .stroke(Color.gray.opacity(0.5), lineWidth: 1)
            }
        }
    }
}

struct RadarDataView: View {
    let data: [Double]
    let size: CGSize
    let color: Color
    
    var body: some View {
        ZStack {
            // 填充区域
            RadarPolygon(
                count: data.count,
                radius: size.width / 2.5,
                center: CGPoint(x: size.width / 2.5, y: size.width / 2.5),
                values: data
            )
            .fill(color.opacity(0.2))
            
            // 边框线
            RadarPolygon(
                count: data.count,
                radius: size.width / 2.5,
                center: CGPoint(x: size.width / 2.5, y: size.width / 2.5),
                values: data
            )
            .stroke(color, lineWidth: 2)
        }
    }
}

// First, let's create a helper struct for the label positions
struct RadarLabelPosition: Identifiable {
    let id: Int
    let label: String
    let position: CGPoint
    
    init(index: Int, label: String, radius: Double, center: CGPoint) {
        self.id = index
        self.label = label
        let angle = Double(index) * .pi * 2 / 5 - .pi / 2
        let x = cos(angle) * (radius + 20) + center.x
        let y = sin(angle) * (radius + 20) + center.y
        self.position = CGPoint(x: x, y: y)
    }
}

struct RadarLabelsView: View {
    let labels: [String]
    let size: CGSize
    
    var labelPositions: [RadarLabelPosition] {
        let radius = size.width / 2.5
        let center = CGPoint(x: radius, y: radius)
        return labels.enumerated().map { index, label in
            RadarLabelPosition(index: index, label: label, radius: radius, center: center)
        }
    }
    
    var body: some View {
        ForEach(labelPositions) { position in
            Text(position.label)
                .font(.caption)
                .position(position.position)
        }
    }
}

struct RadarPolygon: Shape {
    let count: Int
    let radius: Double
    let center: CGPoint
    var values: [Double]?
    
    func path(in rect: CGRect) -> Path {
        var path = Path()
        
        for i in 0..<count {
            let angle = Double(i) * .pi * 2 / Double(count) - .pi / 2
            let value = values?[i] ?? 1.0
            let x = cos(angle) * radius * value + center.x
            let y = sin(angle) * radius * value + center.y
            
            if i == 0 {
                path.move(to: CGPoint(x: x, y: y))
            } else {
                path.addLine(to: CGPoint(x: x, y: y))
            }
        }
        
        path.closeSubpath()
        return path
    }
}



